package mario;

import java.awt.image.BufferedImage;

public class Enemy implements Runnable{
    //坐标
    private int x,y;
    //类型
    private int type;
    //移动方向
    private boolean face_to = true;
    //图像
    private BufferedImage show;
    //背景
    private BackGround bg;
    //食人花极限范围
    private int max_up = 0;
    private int max_down = 0;
    //定义线程对象
    private Thread thread = new Thread(this);
    private int image_type = 0;
    //下落
    public void fall(){
        y -= 10;
    }

    //蘑菇构造函数
    public Enemy(int x, int y, boolean face_to,int type,  BackGround bg) {
        this.x = x;
        this.y = y;
        this.face_to = face_to;
        this.type = type;
        this.bg = bg;
        show = StaticValue.mushroom.get(0);
        thread.start();
    }
    //食人花构造函数
    public Enemy(int x, int y, int type, boolean face_to, int max_up, int max_down ,BackGround bg) {
        this.x = x;
        this.y = y;
        this.type = type;
        this.face_to = face_to;
        this.bg = bg;
        this.max_up = max_up;
        this.max_down = max_down;
        show = StaticValue.corpse.get(0);
        thread.start();
    }
    //死亡方法
    public void death(){
        show = StaticValue.mushroom.get(2);
        this.bg.getEnemyList().remove(this);
    }

    @Override
    public void run() {
        while (true){
            //是否为蘑菇
            if (type==1){
                if (face_to){
                    this.x -= 2;
                }else {
                    this.x += 2;
                }
            }

            image_type = image_type == 0 ? 1 : 0;
            show = StaticValue.mushroom.get(image_type);

            boolean canLeft = true;
            boolean canRight = true;

            for (int i = 0; i < bg.getObstacles().size();i++){
                Obstacle obstacle = bg.getObstacles().get(i);
                //判断是否可以向右走
                if (obstacle.getX() == this.x +30 && (obstacle.getY() + 30 > this.y && obstacle.getY() -30 < this.y)){
                    canRight = false;
                }
                //判断是否可以向左走
                if (obstacle.getX()==this.x-30 && (obstacle.getY() +30 > this.y && obstacle.getY() -30 < this.y)){
                    canLeft = false;
                }
            }

            if (face_to && !canLeft || this.x ==0){
                face_to = false;
            }else if ((!face_to) && (!canRight) || this.x==870 ){
                face_to = true;
            }
            //是否食人花
            if (type==2){
                if (face_to){
                    this.y -= 2;
                }else {
                    this.y += 2;
                }

                //image_type = image_type == 0 ? 1 : 0;
                //食人花是否到达极限
                if (face_to && (this.y == max_up)){
                    face_to = false;
                }
                if ((!face_to) && (this.y ==max_down)){
                    face_to = true;
                }
                show = StaticValue.corpse.get(image_type);
            }

            try {
                Thread.sleep(120);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getType() {
        return type;
    }

    public BufferedImage getShow() {
        return show;
    }
}
